/*
 * 文件名：ContractExtController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月13日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.ContractExtDomain;
import top.infopub.mgr.bpa.jsonVo.ContractExtJsonVo;
import top.infopub.mgr.bpa.jsonVo.ContractReviewJsonVo;
import top.infopub.mgr.bpa.service.BpaContractBpaReviewService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ContractExtService;
import top.infopub.mgr.bpa.vo.ContractExtVo;
import top.infopub.mgr.cap.jsonvo.LookInfoJsonVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.bpa.BpaContractBpaReview;
import top.infopub.model.bpa.BpaContractListing;
import top.infopub.model.bpa.BpaContractNeg;
import top.infopub.model.bpa.ContractExt;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**对外合同
 * @author wuzhiyang
 * @version 2020年2月13日
 * @see ContractExtController
 * @since商务管理/对外合同/承继协议
 */
@Controller
@RequestMapping("/contractExt")
public class ContractExtController extends GenericFlowController {


    @Autowired
    private ContractExtService contractExtService;

    @Autowired
    private BpaContractBpaReviewService bpaContractBpaReviewService;

    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;


    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }

    /**
     * 承继协议列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ContractExtDomain> queryDataByCondition(ContractExtVo contractExtVo,
                                                         ParamPage paramPage) throws ParseException {
        Page<ContractExtDomain> page = new Page<ContractExtDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (contractExtVo.getEndDate() != null) {
            contractExtVo.setEndDate(DateUtils.addDays(contractExtVo.getEndDate(), 1));
        }
        page = contractExtService.selectByDomainWhere(page, contractExtVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        ContractExt contractExt = contractExtService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (contractExt!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractExt.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(contractExt.getProcessId()) && contractExt.getProcessId().length() > 10){
                request.setAttribute("processId", contractExt.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(contractExt.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractExt.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-SuccessionAgreement-detail";
    }
    @RequestMapping("/queryDrop")
    @ResponseBody
    public RspVo queryDrop(){
        //查询已有谈判合同的项目
        List<BpaProjectDomain> bpaProjectDomain = contractExtService.selectProject();
        return RspVo.getSuccessResponse(bpaProjectDomain);
    }

    @RequestMapping("/getDrop")
    @ResponseBody
    public RspVo getDrop(){
        LookInfoJsonVo lookInfoJsonVo = new LookInfoJsonVo();
        return RspVo.getSuccessResponse(lookInfoJsonVo);
    }

    @RequestMapping("/queryExtDetail")
    @ResponseBody
    public RspVo queryExtDetail(String id) {
        //承继协议
        ContractExtJsonVo contractNegJsonVo = contractExtService.queryDetailById(id);
        return RspVo.getSuccessResponse(contractNegJsonVo);
    }

    @RequestMapping("save")
    @ResponseBody
    public RspVo save (String bodyStr, Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        ContractExtJsonVo vo = JSON.parseObject(s,ContractExtJsonVo.class);
        ContractExt contractExt = new ContractExt();
        if (StringUtils.isNotBlank(vo.getContractExt().getId())){//编辑
            BeanUtils.copyProperties(vo.getContractExt(),contractExt);
            contractExt.setUptBy(userDtl.getId());
            contractExt.setUptTime(new Date());
            contractExt.setProCode(vo.getProjectInfo().getProCode());
        }else{//新增
            BeanUtils.copyProperties(vo.getContractExt(),contractExt);
            contractExt.setId(IdFactory.getUUIDUpper());
            contractExt.setProCode(vo.getProjectInfo().getProCode());
            contractExt.setAddBy(userDtl.getId());
            contractExt.setAddTime(new Date());
        }
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,contractExt.getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return contractExtService.saveAll(userDtl,bodyStr,submitType,contractExt,vo.getBpaContractReviewPublic(),vo.getBpaContractSummary(),
                vo.getBpaContractListing(),vo.getBpaContractFormula(),vo.getBpaContractBid(),vo.getBpaContractBusiness(),attachmentList
        );
    }

    @RequestMapping("/saveListing")
    @ResponseBody
    public RspVo saveListing(String bodyStr,String id){
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        List<BpaContractListing> listingJsonVos = JSON.parseArray(s, BpaContractListing.class);
        return bpaContractBpaReviewService.saveListing(listingJsonVos,id);
    }
}
